package cn.tedu.bonfire.front.content.dao.persist.repository.impl;


import cn.tedu.bonfire.front.content.dao.persist.mapper.LikeCollectMapper;
import cn.tedu.bonfire.front.content.dao.persist.repository.ILikeCollectRepository;
import cn.tedu.bonfire.front.content.pojo.entity.LikeCollect;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Slf4j
@Repository
public class LikeCollectRepositoryImpl implements ILikeCollectRepository {

    @Autowired
    private LikeCollectMapper likeCollectMapper;

    /**
     * 点赞/取消点赞后点赞量增加/减少
     * @param articleId
     */
    @Override
    public void updateLikeTimesByArticleId(Long articleId, Integer state) {
        if (state == 0) {
            likeCollectMapper.addLikeTimesByArticleId(articleId);
        } else {
            likeCollectMapper.delLikeTimesByArticleId(articleId);
        }
    }

    /**
     *  收藏/取消收藏后收藏量减少
     * @param articleId
     * @param state
     */
    @Override
    public void updateCollectTimesByArticleId(Long articleId, Integer state) {
        if (state == 0) {
            likeCollectMapper.addCollectTimesByArticleId(articleId);
        } else {
            likeCollectMapper.delCollectTimesByArticleId(articleId);
        }
    }

    @Override
    public Long switchUpdateOrInsertArticle(Long userId, Long articleId) {
        UpdateWrapper<LikeCollect> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.eq("article_id", articleId);
        Long count = likeCollectMapper.selectCount(updateWrapper);
        return count;
    }
    /**
     * 更新文章点赞收藏状态
     * @param likeCollect
     * @return
     */
    @Override
    public int updateLikeCollection(LikeCollect likeCollect) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("state", likeCollect.getState());
        updateWrapper.eq("user_id", likeCollect.getUserId());
        updateWrapper.eq("article_id", likeCollect.getArticleId());
        int row = likeCollectMapper.update(likeCollect, updateWrapper);
        return row;
    }
    /**
     * 保存文章点赞数据到数据库
     * @param likeCollect
     * @return
     */
    @Override
    public int insert(LikeCollect likeCollect) {
        return likeCollectMapper.insert(likeCollect);
    }
}
